package com.winning.gjhlht;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.winning.common.Constans;
import com.winning.common.tools.CDASchemaValidateTool;
import com.winning.core.base.spring.listener.AppBaseModule;
import com.winning.core.job.JobControlInter;
import com.winning.core.net.CXFWebserviceClent;
import com.winning.gjhlht.bi.controller.BIDataUploadController;
import com.winning.gjhlht.ihe.cdaService.controller.CDAController;
import com.winning.gjhlht.ihe.hl7ClientService.controller.TestController;


/**
 * 类名称：GjpthlhtServiceModule
 * 类描述：国家平台-互联互通服务模块
 * 创建人：liuwensheng
 * 创建时间：2016-12-8 下午4:03:05
 * 修改人：liuwensheng
 * 修改时间：2016-12-8 下午4:03:05
 * 修改备注：
 */
@Component
public class GjpthlhtServiceModule extends AppBaseModule {
    private static Logger LOGGER = Logger.getLogger(Constans.LOG4J_PBS_LOGGER_ID);

    //Spring注入依赖(接口具体实现类)，变量名称遵循统一规则,如果有多个bean则需要指定
    @Autowired
    @Qualifier("com.winning.lang.ihe.job.IHEClientJobControler")
    private JobControlInter iHEClientJobControler = null;

    @Override
    protected void initModuleHandler() {


//		CDAController cdaController = new CDAController();
//		cdaController.startCreateCDATest();

        LOGGER.info("互联互通服务模块启动...");
        //初始化作业控制器
        iHEClientJobControler.initJobController();

        //CDASchemaValidateTool.validateCDA("com/winning/ihe/cda/cdaFiles/cda_ehr_grjbjkxxdj_temp.xml_王英 _650502081020001_511304199405163021_6CE09C3A-F6BA-B003-3B23-F55ADACCB7E0.xml", "resource/sdschemas/PCHIS_CDA.xsd");

        //TestController.test();
        //textAxsis();
        //BIDataUploadController biDataUploadController = new BIDataUploadController();
        //biDataUploadController.init();
    }


    public static void main(String[] args) {
        testAxis2();

        //testCXF();
    }

    public static void textAxsis() {
        /**
         String url =" http://113.119.4.5:XXXX/NiLockWebService.asmx?wsdl";
         RPCServiceClient client = newRPCServiceClient();
         Options options = client.getOptions();
         EndpointReference endpoint = newEndpointReference(url);
         options.setTo(endpoint);
         options.setProperty(HTTPConstants.CHUNKED, Boolean.FALSE);
         options.setAction("http://microsoft.com/webservices ");
         options.setProperty(HTTPConstants.SO_TIMEOUT, 600*1000);
         Object[] obj = new Object[]{username,acc,json};

         //返回值类型
         Class<?>[] classes = new Class[] { String.class };

         QName qname = new QName("http://microsoft.com/webservices/ ", "GPRSCMD");

         String result = (String)client.invokeBlocking(qname, obj,classes)[0];
         **/

        try {

            LOGGER.info("测试...");
            URL url = new URL("http://192.168.102.21:6086/gateway/ws/Registry");
            //URL url = new URL("http://172.16.0.29/IHEWinning/services/HIPMessageService");
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("");
            QName QNAME = new QName("urn:hl7-org:v3", "HIPMessageServer");
            call.setOperationName(QNAME);


            call.addParameter(new QName("urn:hl7-org:v3", "action"),
                    org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName("urn:hl7-org:v3", "message"),
                    org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName("urn:hl7-org:v3", "return"),
                    org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.OUT);


            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            String result = (String) call.invoke(new Object[]{"ProvideAndRegisterDocumentSet-b", ""});
            LOGGER.info(result);
            System.out.println(result);
        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());
        }


    }

    public static void testAxis2() {

        String url = "";
        url = "http://192.168.102.21:6086/gateway/ws/Registry";
        Call call;
        try {
            LOGGER.info("测试..." + url);
            Object[] obj = new Object[]{"ProvideAndRegisterDocumentSet-b", "<xml></xml>"};
            Service service = new Service();
            call = (Call) service.createCall();
            call.setTargetEndpointAddress(url);
            call.setEncodingStyle("utf-8");
            call.setOperationName("HIPMessageServer");
            call.addParameter("action", XMLType.XSD_STRING, ParameterMode.IN);
            call.addParameter("message", XMLType.XSD_STRING, ParameterMode.IN);
            call.setReturnType(XMLType.XSD_STRING);
            String result = (String) call.invoke(obj);
            System.out.println(result);
            LOGGER.info("返回结果：" + result);
        } catch (Exception e) {
            LOGGER.info(e, e.fillInStackTrace());
            e.printStackTrace();
        }
    }


    public static void testCXF() {

        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();

        Client client = null;

        Object[] result = null;

        try {


            client = clientFactory.createClient("http://172.16.0.29/IHEWinning/services/HIPMessageService?wsdl");

            LOGGER.info("--> createClient");
            //服务名称固定，参数可变
            result = client.invoke("HIPMessageServer", "ProvideAndRegisterDocumentSet-b", "1111111");
            LOGGER.info("--> invoke");

            if (result != null && result.length > 0) {

                LOGGER.info("--> cxf客户端调用返回结果:" + result[0]);
            } else {
                LOGGER.info("--> cxf客户端调用返回结果为null");
            }


            //关闭连接
            client.destroy();

            /**
             client  = clientFactory.createClient("http://59.255.59.31:8280/services/audit?wsdl");


             //服务名称固定，参数可变
             result = client.invoke("audit", "1111111");


             if(result != null && result.length > 0)
             {
             LOGGER.info("--> 审计消息接口调用返回结果:" + result[0]);
             }
             else
             {
             LOGGER.info("--> 审计消息接口调用返回结果为null");
             }

             //关闭连接
             client.destroy();
             **/
        } catch (Exception e) {
            LOGGER.error(e, e.fillInStackTrace());
            e.printStackTrace();
        }
    }

}
